package swimv2gm.client.user;

import java.io.IOException;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import swimv2gm.bl.session.FriendshipManager;

/**
 * Invia una richiesta di amicizia ad un altro utente.
 * 
 * @author Gallesio Matteo
 */
public class SendFriendship extends HttpServlet {
	private static final long serialVersionUID = 7075013674266337206L;

	protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
		FriendshipManager man;
		try {
			man = (FriendshipManager)InitialContext.doLookup("FriendshipManager");
		} catch (NamingException e) {
			response.sendError(500);
			return;
		}
		boolean success;
		long dest = -1;
		Long mitt = (Long)(request.getSession().getAttribute("user"));
		if (mitt == null) {
			success = false;
		}
		else {
			try {
				dest = Long.parseLong(request.getParameter("dest"));
				success = man.addRequest(mitt,dest);
			} catch (IllegalArgumentException e) {
				success = false;
			}
		}

		response.sendRedirect("/SWIMv2gm/user/Profile?uid=" + dest + "&executed=friendship&success=" + success);
	}

	protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
		doGet(request,response);
	}

}
